/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.score.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.wechat.score.dao.ScoreExchangeDao;
import com.boxin.ims.modules.wechat.score.entity.ScoreExchange;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;

/**
 * 积分对换设置Service
 * @author Jakemanse
 * @version 2013-09-23
 */
@Component
@Transactional(readOnly = true)
public class ScoreExchangeService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(ScoreExchangeService.class);
	
	@Autowired
	private ScoreExchangeDao scoreExchangeDao;
	
	public ScoreExchange get(Long id) {
		return scoreExchangeDao.findOne(id);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-24  下午1:37:51
	 * @function <p>根据交互消息获取积分兑换对应 </p>
	 * @param userId
	 * @param message
	 * @return
	 */
	public ScoreExchange getByMessage(Long userId,String message){
		DetachedCriteria dc = scoreExchangeDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		dc.add(Restrictions.eq("message", message));
		List<ScoreExchange> result = scoreExchangeDao.find(dc);
		return (result == null || result.isEmpty()) ? null : result.get(0);
	}
	
	
	
	public List<ScoreExchange> findAllByUserId(Long userId ){
		DetachedCriteria dc = scoreExchangeDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		dc.add(Restrictions.eq("delFlag", ScoreExchange.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return scoreExchangeDao.find( dc);
		
	}
	
	public Page<ScoreExchange> find(Page<ScoreExchange> page, ScoreExchange scoreExchange) {
		DetachedCriteria dc = scoreExchangeDao.createDetachedCriteria();
		if (scoreExchange.getUser()!=null && scoreExchange.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", scoreExchange.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(scoreExchange.getName())){
			dc.add(Restrictions.like("name", "%"+scoreExchange.getName()+"%"));
		}
		if (StringUtils.isNotEmpty(scoreExchange.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+scoreExchange.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", ScoreExchange.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return scoreExchangeDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(ScoreExchange scoreExchange) {
		scoreExchangeDao.save(scoreExchange);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		scoreExchangeDao.deleteById(id);
	}
	
	
	@Transactional(readOnly = false)
	public void updateStatusById(Long id,String status) {
		scoreExchangeDao.updateStatusById(id,status);
	}
	
}
